#include "tlvSet.h"


int tAna(char *buf){
    char cd[] = "cd";//11
    char ls[] = "ls";//12
    char pwd[] = "pwd";//13
    char mkdir[] = "mkdir";//14
    char rmdir[] = "rmdir";//15
    char putsfile[] = "puts";//21
    char getsfile[] = "gets";//25
    char remove_file[] = "remove";//26
    char register_user[] = "register";//32
    char login[] = "login";//33

    if(strcmp(buf, cd) == 0){
        printf("要发送的是cd\n");
        return 11;
    }
    if(strcmp(buf, ls) == 0){
        printf("要发送的是ls\n");
        return 12;
    }
    if(strcmp(buf, pwd) == 0){
        printf("要发送的是pwd\n");
        return 13;
    }
    if(strcmp(buf, mkdir) == 0){
        printf("要发送的是mkdir\n");
        return 14;
    }
    if(strcmp(buf, rmdir) == 0){
        printf("要发送的是rmdir\n");
        return 15;
    }
    if(strcmp(buf, putsfile) == 0){
        printf("要发送的是puts\n");
        return 21;
    }
    if(strcmp(buf, getsfile) == 0){
        printf("要发送的是gets\n");
        return 25;
    }
    if(strcmp(buf, remove_file) == 0){
        printf("要发送的是remove\n");
        return 26;
    }
    if(strcmp(buf, register_user) == 0){
        printf("要发送的是register\n");
        return 32;
    }
    if(strcmp(buf, login) == 0){
        printf("要发送的是login\n");
        return 33;
    }
    //未来的其他命令 

    return 0;
}


int tlvSend(tlv_data_t *pdata, int sockfd){
    int ret = send(sockfd, pdata, sizeof(pdata->type)+sizeof(pdata->length)+pdata->length, 0);
    ERROR_CHECK(ret , -1, "send failed:");
    printf("发送成功\n");
    return 0;
}


int tlvSet(char *buf[], int size, int sockfd){
    tlv_data_t data;
    if(size == 2){   
        data.type = tAna(buf[0]);
        data.length = strlen(buf[1]);
        memcpy(data.value, buf[1], strlen(buf[1]));
        tlvSend(&data, sockfd);
    }

    if(size == 1){   
        data.type = tAna(buf[0]);
        data.length = 0;
        tlvSend(&data, sockfd);
    }
    return 0;
}

